senate_races_dems <- c('Arizona',  'California' , 'Colorado' , 'Connecticut', 'Georgia' , 'Hawaii', 'Illinois',  'Maryland' , 'Nevada' , 'New Hampshire' , 'New York',  'Oregon' , 'Pennsylvania', 'Vermont',  'Washington') %>% toupper()

senraces_codes <- statecode %>% filter(state %in% senate_races) %>% select(state_code) %>% as.vector() %>% unlist()

senraces_codes_dems <- statecode %>% filter(state %in% senate_races_dems) %>% select(state_code) %>% as.vector() %>% unlist()

final_sen <- final %>% filter(inputstate %in% as.numeric(senraces_codes))
final_sen <- final_sen %>% mutate(sen_won = case_when(inputstate %in% as.numeric(senraces_codes_dems) & pid2 == "Democrat" ~ 1,
                                                      !(inputstate %in% as.numeric(senraces_codes_dems)) & pid2 == "Democrat" ~ 0,
                                                      inputstate %in% as.numeric(senraces_codes_dems) & pid2 == "Republican" ~ 0,
                                                      !(inputstate %in% as.numeric(senraces_codes_dems)) & pid2 == "Republican" ~ 1))

bb1 <- ggplot()+
  geom_smooth(data = final_sen %>% filter(days < 1, days <= 150), aes(days, violence_range,  color = factor(sen_won), fill = factor(sen_won)), method = 'loess')+
  geom_smooth(data = final_sen %>% filter(days > -1, days <= 150), aes(days, violence_range,  color = factor(sen_won), fill = factor(sen_won)), method = 'loess')+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(0,0.4))+
  scale_x_continuous(breaks = seq(-50,150,10))+
  xlab('Days before/after 2022 Election')+
  ylab('Support Political Violence')+
  labs(fill = "Senate Candidate", color = 'Senate Candidate')+
  scale_color_manual(values = c("#cc6677", "#1c9906"),
                     labels=c('Lost', 'Won'))+
  scale_fill_manual(values = c("#cc6677", "#1c9906"),
                    labels=c('Lost', 'Won'))+
  theme(legend.position = 'none')

bb2 <- ggplot()+
  geom_smooth(data = final_sen %>% filter(days < 1, days <= 150), aes(days, norms_range,  color = factor(sen_won), fill = factor(sen_won)), method = 'loess')+
  geom_smooth(data = final_sen %>% filter(days > -1, days <= 150), aes(days, norms_range,  color = factor(sen_won), fill = factor(sen_won)), method = 'loess')+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(0.2,0.6))+
  scale_x_continuous(breaks = seq(-50,150,10))+
  xlab('Days before/after 2022 Election')+
  ylab('Support Norm Violations')+
  labs(fill = "Senate Candidate", color = 'Senate Candidate')+
  scale_color_manual(values = c("#cc6677", "#1c9906"),
                     labels=c('Lost', 'Won'))+
  scale_fill_manual(values = c("#cc6677", "#1c9906"),
                    labels=c('Lost', 'Won'))+
  theme(legend.position = 'none')

bb3 <- ggplot()+
  geom_smooth(data = final_sen %>% filter(days < 1, days <= 150), aes(days, affpol,  color = factor(sen_won), fill = factor(sen_won)), method = 'loess')+
  geom_smooth(data = final_sen %>% filter(days > -1, days <= 150), aes(days, affpol,  color = factor(sen_won), fill = factor(sen_won)), method = 'loess')+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(20,80))+
  scale_x_continuous(breaks = seq(-50,150,10))+
  xlab('Days before/after 2022 Election')+
  ylab('Affective Polarization')+
  labs(fill = "Senate Candidate", color = 'Senate Candidate')+
  scale_color_manual(values = c("#cc6677", "#1c9906"),
                     labels=c('Lost', 'Won'))+
  scale_fill_manual(values = c("#cc6677", "#1c9906"),
                    labels=c('Lost', 'Won'))+
  theme(legend.position = 'bottom')


final_figure_win_sen <- suppressMessages(ggpubr::ggarrange(bb1,bb2,bb3, ncol = 1))
suppressMessages(print(final_figure_win_sen))
#ggsave(final_figure_win_sen, file = 'final_figure_win_sen.png', units = 'in', height = 8, width = 10)